﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Bricks.BaseFramework.GenericControls
{
    /// <summary>
    /// ButtonList.xaml 的交互逻辑
    /// </summary>
    public partial class ButtonList : UserControl
    {
        #region 成员变量
        private ButtonListStyleEntry style;
        public delegate void ButtonClickEventHandler(object sender, ButtonClickEventArgs e);
        public event ButtonClickEventHandler ButtonClick;
        #endregion

        #region 属性
        /// <summary>
        /// 移动方式（可设置值：Line或Page）
        /// </summary>
        private string moveWay = "Page";  //默认为Page
        public string MoveWay
        {
            get
            {
                return moveWay;
            }
            set
            {
                moveWay = value;
            }
        }

        /// <summary>
        /// 按钮列表
        /// </summary>
        private List<Button> buttonElementList = new List<Button>();
        public List<Button> ButtonElementList
        {
            get
            {
                return buttonElementList;
            }
            set
            {
                if (buttonElementList != value)
                {
                    buttonElementList = value;
                    GenerateButtonsUI();
                }
            }
        }

        /// <summary>
        /// Tag封装的对象
        /// </summary>
        public object TagObject { get; set; }
        /// <summary>
        /// 前移（普通）
        /// </summary>
        public string Per_Default { get; set; }
        /// <summary>
        /// 前移（焦点）
        /// </summary>
        public string Per_IsMouseOver { get; set; }
        /// <summary>
        /// 前移（点击）
        /// </summary>
        public string Per_IsPressed { get; set; }
        /// <summary>
        /// 后移（普通）
        /// </summary>
        public string Next_Default { get; set; }
        /// <summary>
        /// 后移（焦点）
        /// </summary>
        public string Next_IsMouseOver { get; set; }
        /// <summary>
        /// 后移（点击）
        /// </summary>
        public string Next_IsPressed { get; set; }

        private bool isNav = false;
        public bool IsNav
        {
            get
            {
                return isNav;
            }
            set
            {
                isNav = value;
            }
        }
        #endregion

        /// <summary>
        /// 构造
        /// </summary>
        public ButtonList()
        {
            InitializeComponent();
            GenerateButtonsUI();
            //MessageBox.Show(string.Format("svContent Width:{0}，spContent Width:{1}", svContent.Width.ToString(), spContent.Width.ToString()));
        }

        #region 事件及方法
        /// <summary>
        /// 控件加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            style = new ButtonListStyleEntry();
            style.Per_Default = this.Per_Default;
            style.Per_IsMouseOver = this.Per_IsMouseOver;
            style.Per_IsPressed = this.Per_IsPressed;
            style.Next_Default = this.Next_Default;
            style.Next_IsMouseOver = this.Next_IsMouseOver;
            style.Next_IsPressed = this.Next_IsPressed;
            this.DataContext = style;
            //MessageBox.Show(svContent.ActualWidth.ToString());
            //MessageBox.Show("Loaded：" + svContent.ContentHorizontalOffset.ToString());
        }

        /// <summary>
        /// 生成Buttons
        /// </summary>
        private void GenerateButtonsUI()
        {
            spContent.Children.Clear();
            bool isAll = true;
            foreach (var item in buttonElementList)
            {
                item.Style = Resources["navButton"] as Style;
                if (IsNav && isAll)
                {
                    LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
                    myLinearGradientBrush.StartPoint = new Point(0, 0);
                    myLinearGradientBrush.EndPoint = new Point(1, 1);
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.DeepSkyBlue, 0.01));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Transparent, 1.0));

                    // Use the brush to paint the rectangle. 
                    item.Background = myLinearGradientBrush;

                    //item.Background = Brushes.GhostWhite;
                    isAll = false;
                }
                else if (!IsNav)
                    item.Cursor = Cursors.Hand;
                //else
                //    item.Background = Brushes.WhiteSmoke;
                spContent.Children.Add(item);
                item.Click += button_Click;
            }
            if (buttonElementList != null && buttonElementList.Count > 0)
                TagObject = buttonElementList[0].Tag;
            //MessageBox.Show("Generate：" + svContent.ContentHorizontalOffset.ToString());
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void button_Click(object sender, RoutedEventArgs e)
        {
            Button senderButton = sender as Button;
            if (senderButton == null)
                return;
            if (ButtonClick != null)
            {
                if (IsNav)
                {
                    foreach (var item in buttonElementList)
                    {
                        item.Background = Brushes.Transparent;
                    }
                    //LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
                    //myLinearGradientBrush.StartPoint = new Point(0, 0);
                    //myLinearGradientBrush.EndPoint = new Point(1, 1);
                    //myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Yellow, 0.0));
                    //myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Red, 0.25));
                    //myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Blue, 0.75));
                    //myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.LimeGreen, 1.0));
                    LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush();
                    myLinearGradientBrush.StartPoint = new Point(0, 0);
                    myLinearGradientBrush.EndPoint = new Point(1, 1);
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Black, 0.0));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.DeepSkyBlue, 0.01));
                    myLinearGradientBrush.GradientStops.Add(new GradientStop(Colors.Transparent, 1.0));

                    // Use the brush to paint the rectangle. 
                    senderButton.Background = myLinearGradientBrush;
                }
                TagObject = senderButton.Tag;  //将Button的Tag封装为TagObject
                ButtonClickEventArgs goEventArgs = new ButtonClickEventArgs();
                ButtonClick(this, goEventArgs);
            }
        }

        /// <summary>
        /// 左移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            if (MoveWay.Equals("Page"))
                svContent.PageLeft();
            else
                svContent.LineLeft();
        }

        /// <summary>
        /// 右移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            if (MoveWay.Equals("Page"))
                svContent.PageRight();
            else
                svContent.LineRight();
        }
        #endregion
    }

    /// <summary>
    /// ButtonClick事件
    /// </summary>
    public class ButtonClickEventArgs : EventArgs
    {
        public ButtonClickEventArgs()
        {

        }
    }

    /// <summary>
    /// ButtonList的Style实体
    /// </summary>
    public class ButtonListStyleEntry
    {
        public string Per_Default { get; set; }
        public string Per_IsMouseOver { get; set; }
        public string Per_IsPressed { get; set; }
        public string Next_Default { get; set; }
        public string Next_IsMouseOver { get; set; }
        public string Next_IsPressed { get; set; }
    }

    /// <summary>
    /// NullOrWhiteSpace和Visible的转换
    /// </summary>
    public class NullOrWhiteSpaceToVisibleConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool isVisible = false;
            if (value is string)
            {
                if (string.IsNullOrWhiteSpace(value as string))
                    isVisible = false;
                else
                    isVisible = true;
            }
            //默认折叠
            Visibility target = Visibility.Collapsed;
            if (true == isVisible)
            {
                target = Visibility.Visible;
            }
            return target;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Visibility visibility = Visibility.Collapsed;
            string target = string.Empty;

            if (value is Visibility)
            {
                visibility = (Visibility)value;
            }

            if (visibility == Visibility.Visible)
            {
                target = "Has Value";
            }
            return target;
        }
    }

    /// <summary>
    /// Double和Visible的转换
    /// </summary>
    public class DoubleToVisibilityConvert : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
                return Visibility.Collapsed;
            if ((double)value > 0)
                return Visibility.Visible;
            else
                return Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
